﻿using System;
using NHibernate.Mapping.ByCode;

namespace Domain.Model.Commons.Atrributes
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class Entity : Attribute
    {
        /// <summary>
        /// Mapping table name
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// Entity title name
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Define cache entity flag
        /// </summary>
        public bool IsCache { get; set; }

        /// <summary>
        /// Default : read-write
        /// </summary>
        public ECacheUsage Usage { get; set; }

        /// <summary>
        ///父类定义 Discriminator : column name
        /// </summary>
        public string Discriminator { get; set; }


        public Entity()
        {
            this.IsCache = false;
            this.Usage = ECacheUsage.NonCache;
        }

        public Entity(string tableName)
            : this()
        {
            this.TableName = tableName;
        }
    }

    public enum ECacheUsage
    {
        NonCache,
        NonstrictReadWrite,
        ReadOnly,
        ReadWrite,
        Transactional
    }

    public enum IdGenerators
    {
        GuidComb,
        Assigned
    }

    /// <summary>
    /// discriminator-value : 默认为类名称
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class SubClass : Attribute
    {
        /// <summary>
        /// 默认为类名称
        /// </summary>
        public string DiscriminatorValue { get; set; }

        public SubClass() { }

        public SubClass(string value)
            : this()
        {
            this.DiscriminatorValue = value;
        }
    }

    [AttributeUsage(AttributeTargets.Property,AllowMultiple=false)]
    public class PrimaryKey : Attribute
    {
        public string ColumnName { get; set; }
        public IdGenerators Generator { get; set; }

        public PrimaryKey()
        {

        }

        public PrimaryKey(string columnName)
            : this()
        {
            this.ColumnName = columnName;
        }
    }

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class Property : Attribute
    {
        public string ColumnName { get; set; }

        public string Title { get; set; }

        public Property()
        {

        }

        public Property(string columnName)
            : this()
        {
            this.ColumnName = columnName;
        }
    }

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class Version : Attribute
    {
        public string ColumnName { get; set; }

        public string Title { get; set; }

        public Version()
        {

        }

        public Version(string columnName)
            : this()
        {
            this.ColumnName = columnName;
        }
    }

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class ManyToOne : Attribute
    {
        public string Title { get; set; }

        public string ColumnName { get; set; }

        public ManyToOne() { }

        public ManyToOne(string columnName)
            : this()
        {
            this.ColumnName = columnName;
        }
    }

    /// <summary>
    /// Mapping collection as an IList instance
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class OneToMany : Attribute
    {
        public string Title { get; set; }
        public string MappingColumn { get; set; }

        /// <summary>
        /// Default : Lazy
        /// </summary>
        public CollectionLazy Lazy { get; set; }

        /// <summary>
        /// Default : Cascade.Persist
        /// </summary>
        public Cascade CascadeStyle { get; set; }

        public OneToMany()
        {
            this.Lazy = CollectionLazy.Lazy;
            this.CascadeStyle = Cascade.Persist;
        }

        public OneToMany(string mappingColumn)
            : this()
        {
            this.MappingColumn = mappingColumn;
        }
    }
}
